<?php

if(!class_exists('fs_admin')){
	class fs_admin{

		private $api;
		private $authorised;
		
		static function menuPage(){
			$options = unserialize(get_option("fs_options"));
			if(empty($options)){ //create some default options
				$options = array(
					"primary" => true,
					"thickbox"=> true,
					"size"    => "s",
					"psize"   => "-"
				);
				update_option("fs_options", serialize($options));
			}
			
			$api = NULL;
			
			if(isset($_POST['action'])) check_admin_referer();
			
			switch($_POST['action']) {
			 	case 'auth': //attempting authorisation.
					//print $_POST['frob'];
					$api = new Phlickr_Api(FS_API_KEY, FS_API_SEC);
					try{
						$token = $api->setAuthTokenFromFrob($_POST['frob']);
						update_option('fs_token', $token);
						print '<div id="message" class="updated fade"><p><strong>Authorisation successful</strong></p></div>';
					}catch(Phlickr_MethodFailureException $e){
						print '<div id="message" class="updated fade"><p><strong>Authorisation failed: '. $e->getMessage() . '</strong></p></div>';
					}
					break;
				
			 	case 'reset': //clearing authorisation
					update_option('fs_token', '');
					print '<div id="message" class="updated fade"><p><strong>Authorisation reset</strong></p></div>';
					break;
					
			 	case 'clear': //clear cache
					file_put_contents(dirname(__FILE__).'/fs_cache', '');
					print '<div id="message" class="updated fade"><p><strong>Cache cleared</strong></p></div>';
					break;
				
				case 'update': //update fs_options.
					$options["primary"]  = ($_POST['primary'])  ? true : false;
					$options["thickbox"] = ($_POST['thickbox']) ? true : false;
					$options["size"] = $_POST['size'];
					$options["psize"] = $_POST['psize'];
					update_option("fs_options", serialize($options));
					print '<div id="message" class="updated fade"><p><strong>Options updated</strong></p></div>';
					break;
				default:
					break;
			}

			extract($options);
			
			if(is_null($api)){
				$token = get_option('fs_token'); 
				$api = (empty($token)) ? 
						new Phlickr_Api(FS_API_KEY, FS_API_SEC) :
			 			new Phlickr_Api(FS_API_KEY, FS_API_SEC, $token);
			}
			
			print '<div class="wrap"><h2>flickrSet Options</h2>';
			print '<style type="text/css">' . fs_admin::css() . '</style>';
			
			if($api->isAuthValid()){
				$user = new Phlickr_User($api, $api->getUserId());?>
				<div id="auth" class="good">
					<p>Authorised as:  '<?=$user->getName();?>'</p>
					<form action="" method="post">
						<p>
							<input type="hidden" name="action" value="reset" />
							<input type="submit" value="Reset Authorisation" />
						</p>
					</form>
				</div>
				
				<?php if(is_writable(dirname(__FILE__).'/fs_cache')){?>
					<div id="cache" class="good">
						<p>Cache is writable</p>
						<form action="" method="post">
						<p>
							<input type="hidden" name="action" value="clear" />
							<input type="submit" value="Clear Cache" />
						</p>
						</form>
				<?php }else{ ?>
					<div id="cache" class="bad">
						<p>Cache is not writable!</p>
				<?php } ?>
				</div>
				<div id="options"><h3>Options</h3>
					<form method="post" action="">
					<p>
						<label>
							<input type="checkbox" name="thickbox" value="true" 
							<?php if($thickbox) print 'checked="checked"';?>/>
							Use thickbox
							<span class="exp">Display large versions of 
							thumbnails using thickbox</span>
						</label>
					<p>
					<p>
						<label>
							<input type="checkbox" name="primary" value="true" 
							<?php if($primary) print 'checked="checked"';?>/>
							Use Primary image
							<span class="exp">Place set primary image before
							other images in the set.</span>
						</label>
					<p>
					<p>Primary size:
					  <ul style="list-style-type:none">
						<li>
						  <label>
							<input type="radio" name="psize" value="s" 
							<?= ($psize == "s")? 'checked="checked"' : '';?>/>
							Square
							<span class="exp">75px square image</span>
						  </label>
						</li>
						<li>
						  <label>
							<input type="radio" name="psize" value="t" 
							<?= ($psize == "t")? 'checked="checked"' : '';?>/>
							Thumbnail
							<span class="exp">100px longest side</span>
						  </label>
						</li>
						<li>
						  <label>
							<input type="radio" name="psize" value="m" 
							<?= ($psize == "m")? 'checked="checked"' : '';?>/>
							Small
							<span class="exp">240px longest side</span>
						  </label>
						</li>
						<li>
						  <label>
							<input type="radio" name="psize" value="-" 
							<?= ($psize == "-")? 'checked="checked"' : '';?>/>
							Medium
							<span class="exp">500px longest side</span>
						  </label>
						</li>
						<li>
						  <label>
							<input type="radio" name="psize" value="b" 
							<?= ($psize == "b")? 'checked="checked"' : '';?>/>
							Large
							<span class="exp">1024px longest side</span>
						  </label>
						</li>
					  </ul>
					<p>
					<p>Thumbnail size:
					  <ul style="list-style-type:none">
						<li>
						  <label>
							<input type="radio" name="size" value="s" 
							<?= ($size == "s")? 'checked="checked"' : '';?>/>
							Square
							<span class="exp">75px square image</span>
						  </label>
						</li>
						<li>
						  <label>
							<input type="radio" name="size" value="t" 
							<?= ($size == "t")? 'checked="checked"' : '';?>/>
							Thumbnail
							<span class="exp">100px longest side</span>
						  </label>
						</li>
						<li>
						  <label>
							<input type="radio" name="size" value="m" 
							<?= ($size == "m")? 'checked="checked"' : '';?>/>
							Small
							<span class="exp">240px longest side</span>
						  </label>
						</li>
					  </ul>
					<p>
					<input type="hidden" name="action" value="update" />
					<p class="submit">
						<input type="submit" name="Submit" value="<?php _e('Update Options »') ?>" />
					</p>
				</form></div><?php
				
			}else{
				$f = $api->requestFrob();
				$url = $api->buildAuthURL('read', $frob);?>
				<div id="noauth" class="bad">
					<h3>flickrSet is not currently authorised!</h3>
					<p>
						<span>Click 'Authorise flickrSet'to allow flickrSet to 
						use your flickr account. Then click 'Confirm 
						Authorisation' to begin using flickrSet.</span>
						<a href="<?=htmlentities($url)?>" target="_blank">Authorise flickrSet</a>
					</p>
					<form action="" method="post">
						<p>
							<input type="hidden" name="action" value="auth" />
							<input type="hidden" name="frob" value="<?=$f?>" />
							<input type="submit" value="Confirm Authorisation" />
						</p>
					</form>
				</div><?php
			}
			?></div><?php //close wrap div
		}
		
		static function css(){
return <<<CSS

	.good {
		background-color: #effff4;
		border:1px solid #66cc87;
	}
	
	.bad {
		border:1px solid #c69;
		background-color: #ffeff7;
	}
	
	div#noauth{
		width:400px;
		margin:10px auto 10px auto;
		padding: 0 10px 10px 10px;
	}

	div#noauth span{
		float:right
		text-align:justify;
		width:190px !important;
	}

	div#noauth p{
		margin: 0 0 25px 20px;
	}

	div#noauth input{
		width:170px;
	}

	div#noauth p a{
		display:block;
		margin-left: 28px ;
	}
	
	div#auth {
		width:300px;
		text-align:center;
		font-weight:bold;
		float: right;
		margin:10px;
		padding: 10px 0;
	}
	
	div#cache{
		text-align:center;
		float:right;
		clear:right;
		width:300px;
		margin:10px;
		padding: 10px 0;
	}
	
	div#options{
		width:650px;
	}
	
	div#options p{
		margin-left: 10px;
	}
	
	div#options p.submit{
		margin-left: 0px;
	}

	div#options label span.exp{
		font-size:0.9em;
		color:#999;
	}

	div#options label span.exp:before{
		content: " - ";
	}
CSS;
}

		//Add the menu to the options page.
		static function addMenu(){
			add_options_page('flickrSet Options', 'flickrSet', 8, 'flickrsetmenu', array('fs_admin','menuPage'));
		}
	}
}

?>